package cn.chencq.moudle.job.task;

import java.util.Date;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;

import cn.chencq.moudle.lottery.entity.LotteryAnalysis;
import cn.chencq.moudle.lottery.service.LotteryAnalysisService;
import cn.chencq.utils.DateUtils;

@Component("lotteryAnalysisTask")
public class LotteryAnalysisTask {

	private Logger logger = LoggerFactory.getLogger(getClass());

	@Autowired
	private LotteryAnalysisService lotteryAnalysisService;

	public void analysis(String param) {
		JSONObject json = JSONObject.parseObject(param);

		String lotteryDate = json.getString("lotteryDate");
		String lotteryType = json.getString("lotteryType");

		if (StringUtils.isBlank(lotteryDate)) {
			Date now = new Date();
			Date yesterday = DateUtils.addMinute(now, -10); // 前十分钟的日期
			lotteryDate = DateUtils.date2Str(yesterday, "yyyy-MM-dd");
		}

		LotteryAnalysis la = lotteryAnalysisService.groupCount(lotteryDate, lotteryType);
		logger.info("统计分析, lotteryDate:{}, lotteryType:{}", lotteryDate, lotteryType);

		lotteryAnalysisService.saveOrUpdate(la);
	}
}
